package com.pmcc.yn.service;
import com.pmcc.utils.AntdPage;
import com.pmcc.utils.BeanUtils;
import com.pmcc.utils.ResultBean;
import com.pmcc.yn.entity.OrderList;
import com.pmcc.yn.entity.UpcomingInfo;
import com.pmcc.yn.repository.OrderListRepository;
import com.pmcc.yn.repository.UpcomingInfoRepository;
import com.pmcc.yn.vo.TaskMessageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by chuxuyang on 2019/5/22.
 */
@Service
@Transactional
public class UpcomingInfoService {

     @Autowired
     UpcomingInfoRepository repository;
    @Autowired
    OrderListRepository orderListRepository;

    public ResultBean showList(AntdPage antdPage , UpcomingInfo model) throws Exception{
        ResultBean resultBean=new ResultBean();
        Page page=repository.findAll(this.getSpecification(model),antdPage.getPageRequest());

        resultBean.setPage(antdPage.getDefaultCurrent());
        resultBean.setTotal(page.getTotalElements());
        resultBean.setResultCode(ResultBean.SUCCESS);
        resultBean.setPageSize(antdPage.getPageSize());
        resultBean.setRows(page.getContent());
        return resultBean;
    }
    public Specification<UpcomingInfo> getSpecification(UpcomingInfo model) {
        return new Specification<UpcomingInfo>() {
            @Override
            public Predicate toPredicate(Root<UpcomingInfo> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
                List<Predicate> list = new ArrayList<Predicate>();
                Predicate[] p = new Predicate[list.size()];
                return builder.and(list.toArray(p));
            }
        };
    }
    /**
     * 新增
     * @return
     */
    public ResultBean add(UpcomingInfo model){
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try{
            model.setCreateTime(new Date());
            repository.saveAndFlush(model);
            resultBean.setResultCode(ResultBean.SUCCESS);
            resultBean.setResultDesc("新增成功");
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }
    /**
     * 修改
     * @param map
     * @return
     */
    public ResultBean update(Map map) {
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            UpcomingInfo c = repository.findById(map.get("id").toString());
            BeanUtils.setBeanData(c,map);
            repository.saveAndFlush(c);
            resultBean.setResultCode(ResultBean.SUCCESS);
            resultBean.setObject(c);
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }
    /**
     * 删除
     * @param id
     * @return
     */
    public ResultBean deleteById(String id) {
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            repository.delete(id);
            resultBean.setResultCode(ResultBean.SUCCESS);
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }
    public ResultBean getMessage(String id) {
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            //待办信息列表
            List<UpcomingInfo> list = repository.findByTechIdOrderByCreateTimeDesc(id);
            List<TaskMessageVo> listVo = new ArrayList<>();
            for (UpcomingInfo upcomingInfo:list){
                //消息实体
                TaskMessageVo vo = new TaskMessageVo();
                //订单id
                vo.setOrderId(upcomingInfo.getOrderId());
                //创建时间
                vo.setCreateTime(upcomingInfo.getCreateTime());
                //通过id查找订单
                OrderList orderList = orderListRepository.findById(upcomingInfo.getOrderId());
                //标题
                vo.setTitle(orderList.getStoreName()+orderList.getSerType());
                //消息分类
                vo.setType(upcomingInfo.getType());
                //内容
                vo.setContent(upcomingInfo.getContent());
                listVo.add(vo);
            }
            resultBean.setRows(listVo);
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);

        }

        return resultBean;
    }


}
